﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DAL.Entities;
using System.Data.SqlClient;

namespace DAL.EntitiesDAL
{
    public class CochinhtriDAL : BaseDAL, UTL.IBaseDAL
    {
        #region Implement

        public int Count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            return ExecuteQuery("sp_SelectAll_Cochinhtri");
        }

        public DataTable Select_Chinhtri()
        {
            return ExecuteQuery("sp_SelectAll_Chinhtri");
        }

        public DataTable Select_Chungnhan()
        {
            return ExecuteQuery("sp_SelectAll_Chungnhan");
        }

        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            var sql = "sp_Delete_Cochinhtri '{0}'";
            sql = string.Format(sql, id);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Delete_Chinhtri(string id, string id1)
        {
            var sql = "sp_Delete_Cochinhtri '{0}', '{1}'";
            sql = string.Format(sql, id, id1);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Insert(object obj)
        {
            var o = (Cochinhtri)obj;
            var sql = "set dateformat dmy; execute sp_Insert_Cochinhtri '{0}', '{1}', N'{2}', '{3}', N'{4}' ";
            sql = string.Format(sql, o.MA_TDCT, o.MANHANVIEN, o.CHUNGNHAN, o.NGAYCAP_CNCT.ToString("dd/MM/yyyy"), o.NOICAP_CNCT);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            var o = (Cochinhtri)obj;
            var sql = "set dateformat dmy; execute sp_Update_Cochinhtri '{0}', '{1}', N'{2}', '{3}', N'{4}' ";
            sql = string.Format(sql, o.MA_TDCT, o.MANHANVIEN, o.CHUNGNHAN, o.NGAYCAP_CNCT.ToString("dd/MM/yyyy"), o.NOICAP_CNCT);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public DataTable Search(string name)
        {
            var sql = @"Select * from tb_Cochinhtri";
            sql = string.Format(sql, name);
            return ExecuteQuery(sql);
        }

        public DataTable Search(DAL.Entities.Cochinhtri o)
        {
            string sql;
            if (o.MA_TDCT != "")
                sql = @"Select * from tb_Cochinhtri Where MA_TDCT like '%" + o.MA_TDCT + "%'  ";
            else if (o.MANHANVIEN != "")
                sql = @"Select * from tb_Cochinhtri Where MANHANVIEN like '%" + o.MANHANVIEN + "%'";
            else if (o.CHUNGNHAN != "")
                sql = @"select * from tb_Cochinhtri where CHUNGNHAN like N'%" + o.CHUNGNHAN + "%' ";
            else
                sql = "@select * from tb_Cochinhtri where NOICAP_CNCT like '%" + o.NOICAP_CNCT + "%' ";

            return ExecuteQuery(sql);
        }


        #endregion
    }
}
